import os
import time

import requests
from bs4 import BeautifulSoup

"""
普通方式爬取https://www.pkdoutu.
"""

headers = {"User-Agent": "Your User Agent"}  # 替换为您的用户代理

data_original_values = []  # 存储所有页面的data-original属性值

# 遍历每一页的URL
for page in range(1, 3):  # 假设要获取前10页的数据
    url = f"https://www.pkdoutu.com/article/list/?page={page}"
    response = requests.get(url, headers=headers)
    html = response.content
    soup = BeautifulSoup(html, 'html.parser')
    images = soup.find_all('img')
    # 提取当前页面的data-original属性值，并添加到列表中
    data_original_values.extend([img['data-original'] for img in images if 'data-original' in img.attrs])

# 开始时间
start_time = time.time()

# 遍历图片地址列表
for image_url in data_original_values:
    # 发送GET请求下载图片
    response = requests.get(url=image_url, headers=headers)

    # 检查响应状态码
    if response.status_code == 200:
        # 提取文件名
        filename = image_url.split("/")[-1]

        # 保存图片到指定路径
        file_path = os.path.join(f"./img/{filename}")
        with open(file_path, 'wb') as f:
            f.write(response.content)
        print(f"图片 {filename} 下载成功！")
    else:
        print(f"图片 {image_url} 下载失败！")

# 结束时间
end_time = time.time()

# 计算下载时间
download_time = end_time - start_time
print(f"下载完成！总共耗时：{download_time:.2f} 秒")
